package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.comon.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;

import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author 七安
 * @description 针对表【dish(菜品管理)】的数据库操作Service实现
 * @createDate 2022-08-09 19:59:19
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish>
        implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    //分页查询
    @Override
    public Page<Dish> findPage(Integer page, Integer pageSize, String name) {
        //条件构造器
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);
        //创建分页对象
        Page<Dish> dishPage = new Page<>(page, pageSize);
        //分页查询
        dishPage = dishMapper.selectPage(dishPage, wrapper);
        //获取查询到的结果集合
        List<Dish> dishList = dishPage.getRecords();
        //创建口味对象
        LambdaQueryWrapper<DishFlavor> dishFlavorWrapper = new LambdaQueryWrapper<>();
        //遍历集合
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //根据菜品中category_id查询对应的菜系名
                Category category = categoryMapper.selectById(dish.getCategoryId());
                dish.setCategoryName(category.getName());

                //获取口味信息
                dishFlavorWrapper.eq(DishFlavor::getDishId, dish.getId());
                //放进dish对象中
                dish.setFlavors(dishFlavorMapper.selectList(dishFlavorWrapper));
            }
        }
        return dishPage;
    }

    @Override
    public void saveDish(Dish dish) {
        //添加菜品
        dishMapper.insert(dish);

        //获取口味
        List<DishFlavor> flavors = dish.getFlavors();
        //将口味添加到口味表
        if (CollectionUtil.isNotEmpty(flavors)) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
                dishFlavorMapper.insert(flavor);
            }
        }
    }

    //修改
    @Override
    public void updateDish(Dish dish) {
        dishMapper.updateById(dish);
        //查询原来对应的口味
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> oldFlavors = dishFlavorMapper.selectList(wrapper);


        //获取口味
        List<DishFlavor> newFlavors = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(newFlavors)) {
            for (DishFlavor flavor : newFlavors) {
                for (DishFlavor oldFlavor : oldFlavors) {
                    //判断名字是否相等
                    if (flavor.getName().equals(oldFlavor.getName())) {
                        flavor.setId(oldFlavor.getId());
                        flavor.setDishId(dish.getId());
                    }
                    //如果原口味的id跟要修改的口味id一致,便修改(新值替换老值)
                    if (Objects.equals(flavor.getId(), oldFlavor.getId())) {
                        dishFlavorMapper.updateById(flavor);
                    }
                }
                //如果不存在就新增
                if (flavor.getDishId() == null) {
                    flavor.setDishId(dish.getId());
                    dishFlavorMapper.insert(flavor);
                }
            }
        }

    }

    /* //修改
    @Override
    public void updateDish(Dish dish) {
        dishMapper.updateById(dish);
        //删除原来对应的口味
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorMapper.delete(wrapper);
        //获取口味
        List<DishFlavor> flavors = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(flavors)) {
            for (DishFlavor flavor : flavors) {
                //清空前端传来的id
                flavor.setId(null);
                dishFlavorMapper.insert(flavor);
            }
        }

    }*/

    //批量删除与单删
    @Override
    public void deleteByIds(List<Long> ids) {
        //判断是否为停售状态
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getStatus, 1);
        wrapper.in(Dish::getId, ids);
        Long status = dishMapper.selectCount(wrapper);
        if (status > 0) {
            throw new CustomException("当前要删除的菜品处于在售状态,请删除后重试");
        }
        //删除
        LambdaQueryWrapper<DishFlavor> dishWrapper = new LambdaQueryWrapper<>();
        for (Long id : ids) {
            //先删口味
            dishWrapper.eq(DishFlavor::getDishId, id);
            dishFlavorMapper.delete(dishWrapper);
            //再删菜品
            dishMapper.deleteById(id);
        }
    }


    //app端查询菜品
    @Override
    public List<Dish> findDish(Long categoryId, Integer status) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, categoryId)
                .eq(Dish::getStatus, status);
        List<Dish> dishes = dishMapper.selectList(wrapper);
        //查询每个菜品的口味信息
        if (CollectionUtil.isNotEmpty(dishes)) {
            for (Dish dish : dishes) {
                //封装条件
                LambdaQueryWrapper<DishFlavor> dishFlavorWrapper = new LambdaQueryWrapper<>();
                dishFlavorWrapper.eq(DishFlavor::getDishId, dish.getId());
                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(dishFlavorWrapper);
                dish.setFlavors(dishFlavorList);
            }
        }
        return dishes;
    }
}





